package com.collect.tanzania.zhiScene.zhiAuditer.zhiFollow.zhiD0;

import android.content.Context;

import com.collect.tanzania.zhiBase.BasePresenter;
import com.collect.tanzania.zhiBase.BaseSubscriber;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCustomerBankAccountEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCustomerRepayOrderListEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRPartRepayListEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRRepaymentLinkEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRWarnLetterEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISCaseRecordEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISCustomerMarkedOrder;
import com.collect.tanzania.zhiBean.zhiSend.ZHISCustomerRepayOrderListEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISPartRepayListEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISRepaymentLinkEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISSendSmsEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISSendWarnLetterEntity;
import com.collect.tanzania.zhiNetApi.ZHIHttpApiProvider;
import com.collect.tanzania.zhiNetApi.ZHIHttpResponse;
import com.collect.tanzania.zhiUtils.RxSchedulers;

import java.util.ArrayList;

import javax.inject.Inject;

public class ZHIAuditerD0Presenter extends BasePresenter<ZHIAuditerD0Contract.View> implements ZHIAuditerD0Contract.Presenter {

    @Inject
    public ZHIAuditerD0Presenter() {

    }

    @Override
    public void getLatestCustomerRepayOrderListV2(Context context, final ZHISCustomerRepayOrderListEntity zhiSCustomerRepayOrderListEntity, boolean showLoading) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRCustomerRepayOrderListEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRCustomerRepayOrderListEntity>>>(context, this, showLoading) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRCustomerRepayOrderListEntity> zhirRCustomerRepayOrderListEntityArrayList = (ArrayList<ZHIRCustomerRepayOrderListEntity>) object;
                            mView.onGetLatestCustomerRepayOrderListV2(zhirRCustomerRepayOrderListEntityArrayList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().customerRepayOrderList(zhiSCustomerRepayOrderListEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void markCustomerRepayOrder(Context context, ZHISCustomerMarkedOrder zhiSCustomerMarkedOrder, boolean showLoading) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, showLoading) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onMarkCustomerRepayOrder();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().markCustomerRepayOrder(zhiSCustomerMarkedOrder)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getPartRepayList(Context context, ZHISPartRepayListEntity zhiSPartRepayListEntity) {
        BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRPartRepayListEntity>>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ArrayList<ZHIRPartRepayListEntity>>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ArrayList<ZHIRPartRepayListEntity> arrayList = (ArrayList<ZHIRPartRepayListEntity>) object;
                            mView.onGetPartRepayList(arrayList);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().partRepayList(zhiSPartRepayListEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getUserVirtualAccount(Context context, final ZHISPartRepayListEntity zhiSPartRepayListEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRCustomerBankAccountEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRCustomerBankAccountEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRCustomerBankAccountEntity zhirCustomerBankAccountEntity = (ZHIRCustomerBankAccountEntity) object;
                            mView.onGetUserVirtualAccount(zhiSPartRepayListEntity, zhirCustomerBankAccountEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().getUserVirtualAccount(zhiSPartRepayListEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }


    @Override
    public void getRepayLink(Context context, ZHISRepaymentLinkEntity zhiSRepaymentLinkEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRRepaymentLinkEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRRepaymentLinkEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRRepaymentLinkEntity zhirRepaymentLinkEntity = (ZHIRRepaymentLinkEntity) object;
                            mView.onGetRepayLink(zhirRepaymentLinkEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().getRepayLink(zhiSRepaymentLinkEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void caseRecordForWhatsapp(Context context, final ZHISCaseRecordEntity zhiSCaseRecordEntity, final String content) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onCaseRecordForWhatsapp(zhiSCaseRecordEntity.getContactMobile(), content);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().caseRecordV2(zhiSCaseRecordEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void sendTagSmsV2(Context context, ZHISSendSmsEntity zhiSSendSmsEntity) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onSendTagSmsV2();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().sendTagSmsV2(zhiSSendSmsEntity)
                .compose(RxSchedulers.<ZHIHttpResponse>applySchedulers())
                .compose(mView.<ZHIHttpResponse>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void sendTagSmsLink(Context context, ZHISSendSmsEntity zhisSendSmsEntity) {
        BaseSubscriber<ZHIHttpResponse> subscriber = new
                BaseSubscriber<ZHIHttpResponse>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            mView.onSendTagSmsLink();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().sendTagSmsLink(zhisSendSmsEntity)
                .compose(RxSchedulers.<ZHIHttpResponse>applySchedulers())
                .compose(mView.<ZHIHttpResponse>bindToLife())
                .subscribe(subscriber);
    }

    @Override
    public void getUserDemandLetterPdfUrl(Context context, ZHISSendWarnLetterEntity zhisSendWarnLetterEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRWarnLetterEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRWarnLetterEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRWarnLetterEntity zhirWarnLetterEntity = (ZHIRWarnLetterEntity) object;
                            mView.onGetUserDemandLetterPdfUrl(zhirWarnLetterEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().getUserDemandLetterPdfUrl(zhisSendWarnLetterEntity)
                .compose(RxSchedulers.applySchedulers())
                .compose(mView.bindToLife())
                .subscribe(subscriber);
    }
}
